#include "ShaderProgram.h"

// Glew.
#define GLEW_STATIC
#include <glew.h>
#ifdef _WIN32
#include <wglew.h>

// OpenGL.
#include <GL/GL.h>
#else
#include <GL/gl.h>
#endif

// CoreLib.
#include "Logging.h"

le::ShaderProgram::ShaderProgram( uint32 vertexShaderId, uint32 fragmentShaderId, uint32 shaderProgramId )
: m_VertexShaderID(vertexShaderId)
, m_FragmentShaderID(fragmentShaderId)
, m_ShaderProgramID(shaderProgramId)
{

}

le::ShaderProgram::~ShaderProgram()
{
	glDetachShader(m_ShaderProgramID, m_VertexShaderID);
	glDetachShader(m_ShaderProgramID, m_FragmentShaderID);

	// Delete the vertex and fragment shaders.
	glDeleteShader(m_VertexShaderID);
	glDeleteShader(m_FragmentShaderID);

	// Delete the shader program.
	glDeleteProgram(m_ShaderProgramID);

	InvalidateShader();
}

void le::ShaderProgram::InvalidateShader()
{
	m_VertexShaderID = 0;
	m_FragmentShaderID = 0;
	m_ShaderProgramID = 0;
}

bool le::ShaderProgram::SetShaderVariable( const char* variableName, const float* mat4x4 )
{
	// Set the projection matrix in the vertex shader.
	unsigned int variableLocation = glGetUniformLocation(m_ShaderProgramID, variableName);
	if(variableLocation == -1)
	{
		LOG(eLog_Warning, "Failed to set the shader variable: %s", variableName);
		return false;
	}
	glUniformMatrix4fv(variableLocation, 1, false, mat4x4);
	return true;
}
